package com.trkj.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.trkj.dao.AttendanceDAO;
import com.trkj.dao.AttendanceparamDAO;
import com.trkj.pojo.Attendance;
import com.trkj.pojo.Attendanceparam;
import com.trkj.pojo.Emp;
import com.trkj.service.AttendanceService;
import com.trkj.utils.DataGrid;
import com.trkj.vo.LoanEmpVo;

@Controller
@SessionAttributes({"emp", "funcIds"})
public class AttendanceController {
	@Autowired
	AttendanceService attendanceService;
	@Autowired
	AttendanceparamDAO attendanceparamDAO;
	@Autowired
	AttendanceDAO attendanceDAO;
	/**
	 * 查看考勤设定的时间
	 * @return
	 * author skm
	 */
	@RequestMapping("/getAttendancePramas")
	@ResponseBody
	public Map<String, Object> getAttendancePramas() {
		Attendanceparam attp = attendanceparamDAO.findByAttparamState(true);
		Map<String, Object> map = new HashMap<>();
		SimpleDateFormat sf = new SimpleDateFormat("HH:mm");
		map.put("attparamLate", sf.format(attp.getAttparamLate()));
		map.put("attparamLeaveearly", sf.format(attp.getAttparamLeaveearly()));
		return map;
	}
	
	
	/**
	 *签到
	 * @param emp
	 * @param attendance
	 * @param attendanceId
	 * @return
	 * @author skm
	 */
	@RequestMapping("to_sava_Attendance_Checkin")
	@ResponseBody
	public Map<String, Object> to_sava_Attendance_Checkin(Model model){
		Emp emp = (Emp)model.asMap().get("emp");//session中获取emp对象
		Attendance attendance = attendanceService.qiandao(emp);//调用服务，进行签到操作，返回一个考勤记录对象
		Map<String, Object> map = new HashMap<>();//考勤记录的两个字段封装到map中，给jsp页面获取值
		SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
		map.put("onworktime",  sf.format(attendance.getAttendanceOnworktime()));
		map.put("attypeId", attendance.getAttendancetype().getAttypeId());
		return map;
	}
	
	
	/**
	 *签退
	 * @param emp
	 * @param attendance
	 * @param attendanceId
	 * @return
	 * @author skm
	 */
	@RequestMapping("to_sava_Attendance_Checkout")
	@ResponseBody
	public Map<String, Object> to_sava_Attendance_Checkout(Model model){
		Emp emp = (Emp)model.asMap().get("emp");//session中获取emp对象
		Attendance attendance = attendanceService.qiantui(emp);//调用服务，进行签到操作，返回一个考勤记录对象
		Map<String, Object> map = new HashMap<>();//考勤记录的两个字段封装到map中，给jsp页面获取值
		SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
		if (attendance!=null) {
			map.put("offworktime",  sf.format(attendance.getAttendanceOffworktime()));
			map.put("attypeId", attendance.getAttendancetype().getAttypeId());
		}
		return map;
	}
	
	/**
	 * 查看签到时间
	 * @param model
	 * @return
	 * @author skm
	 */
	@RequestMapping("to_look_Attendance_Checkin")
	@ResponseBody
	public Map<String, Object> to_look_Attendance_Checkin(Model model){
		Emp emp = (Emp)model.asMap().get("emp");//session中获取emp对象
		Attendance attendance = attendanceService.look_Attendance(emp);
		Map<String, Object> map = new HashMap<>();//考勤记录的两个字段封装到map中，给jsp页面获取值
		if(attendance != null) {
			SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
			map.put("onworktime",  sf.format(attendance.getAttendanceOnworktime()));
			map.put("attypeId", attendance.getAttendancetype().getAttypeId());
		}
		return map;
	}
	
	/**
	 * 查看签退时间
	 * @param model
	 * @return
	 * @author skm
	 */
	@RequestMapping("to_look_Attendance_Checkout")
	@ResponseBody
	public Map<String, Object> to_look_Attendance_Checkout(Model model){
		Emp emp = (Emp)model.asMap().get("emp");//session中获取emp对象
		Attendance attendance = attendanceService.look_Attendance(emp);
		Map<String, Object> map = new HashMap<>();//考勤记录的两个字段封装到map中，给jsp页面获取值
		if(attendance != null) {
			SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
			map.put("offworktime",  sf.format(attendance.getAttendanceOffworktime()));
			map.put("attypeId", attendance.getAttendancetype().getAttypeId());
		}
		return map;
	}
	
	/**
	 * 跳转到个人考勤记录
	 * @return
	 */
	@RequestMapping("to_OnecheckWork")
	public String to_OnecheckWork() {
		return "to_OnecheckWork";
	}

	/**
	 * 查询个人考勤记录
	 * @param model
	 * @param starttime
	 * @param endtime
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @return
	 */
	@RequestMapping("to_findOneAllCheckInfo")
	@ResponseBody
	public DataGrid tofindOneAllCheckInfo(Model model, Date starttime, Date endtime, Integer page, Integer rows, 
			 @RequestParam(required = false, defaultValue = "attendanceId") String sort,
			 @RequestParam(required = false, defaultValue = "desc") String order) {
		Emp emp = (Emp)model.asMap().get("emp");//session中获取emp对象
		return attendanceService.findOneAllCheckInfo(emp, starttime, endtime, page, rows, sort, order);
	}
	
	@RequestMapping("to_OnePersonalLoan")
	public String to_OnePersonalLoan() {
		return "to_OnePersonalLoan";
	}

	/**
	 * 查询借款信息
	 * @author csj
	 * @param empId
	 * @param onetime
	 * @param lasttime
	 * @return
	 */
	@RequestMapping("/to_oneloanlogempInfo")
	public @ResponseBody List<LoanEmpVo> to_oneloanlogempInfo(Model model) {
		Emp emp = (Emp)model.asMap().get("emp");//session中获取emp对象
		List<LoanEmpVo> list = attendanceService.oneloanlogInfo(emp.getEmpId());
		return list;
	}
}
